package cn.tedu._02notice.controller;

import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/notice/")
public class NoticeController {
    //自动装配
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 1.添加资讯功能
     */
    @PostMapping("add")

    public String add(NoticeAddParam noticeAddParam){
        System.out.println("noticeAddParam = " + noticeAddParam);
        //目标:将资讯信息添加到数据库的notice表中;
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);

        //处理其他属性
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        //调用接口层方法
        noticeMapper.insertNotice(notice);

        return "新增资讯成功";
    }

    /**
     * 2.资讯列表功能
     */
    @GetMapping("/v1/notice/list")
    //@ResponseBody
    public List<NoticeListVO> list(NoticeAddQuery noticeAddQuery){
        System.out.println("noticeAddQuery = " + noticeAddQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeList(noticeAddQuery);

        return noticeListVOS;
    }

    /**
     * 3.资讯详情功能
     */
    @GetMapping("/v1/notice/detail")
    //@ResponseBody
    public NoticeDetailInfoVO detail(Long id){
        System.out.println("id = " + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectDetail(id);

        return noticeDetailInfoVO;
    }
    /**
     * 4.删除资讯功能
     */
    @DeleteMapping("/v1/notice/delete")
    //@ResponseBody
    public String delete(Long id){
        System.out.println("id= "+id);
        int rows=noticeMapper.deleteById(id);
        if (rows>0){
            return "删除资讯成功";
        }
        return "删除失败,该资讯不存在";
    }
    @PostMapping("/v1/notice/update")
    //@ResponseBody
    public String update(NoticeUpdateParam noticeUpdateParam){
        System.out.println("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        //处理更新时间
        notice.setUpdateTime(new Date());
        //调用接口
        int rows = noticeMapper.updateNotice(notice);
        if (rows > 0){
            return "更新资讯成功";
        }
        return "更新失败,该资讯不存在";
    }
}

